# -*- coding: utf-8 -*-
 
import sys, os
import multiprocessing
import logging
import random
import time, datetime
from pymongo import MongoClient
from bson import json_util
from bs4 import BeautifulSoup
import urllib
import urllib2
import httplib
import requests

reload(sys)
sys.setdefaultencoding('utf-8')
class Mongo2Api(object):
    mongo_host = '192.168.12.149'
    mongo_port = 27017
    mongo_db = 'novelSpider'

    mongo = None
    mongodb = None
 
    # api_base_url = 'http://xsapi.5kb.pw/api.php/'
    api_base_url = 'http://127.0.0.1:8080/api.php/'

    def __init__(self, logger):
        self.logger = logger
 
        self.mongo = MongoClient(host=self.mongo_host, port=self.mongo_port)
        self.mongodb = self.mongo[self.mongo_db]

    def CalcCNTNovelData(self,ifCalc=True):
        if ifCalc:
            records_cnt = self.mongodb.NovelData.count()
            print "当前小说总数据%d" % records_cnt
            i = 0
            for novel in self.mongodb.NovelData.find():
                title = novel['title']
                print "正在处理第%d条小说《%s》数据" % ((i+1),title)
                self.CalcCNTChapterData(title)
                i = i + 1

        cnt = self.mongodb.NovelData.find({'got_cnt': {'$gt': 0}}).count()
        i = 0
        for j in range(0,cnt / 10):
            for novel in self.mongodb.NovelData.find({'got_cnt':{'$gt':0},'upfile':0}).skip(j*10).limit(10):
                title = novel['title']
                
                print "正在处理上传第%d条小说《%s》数据" % ((i+1), title)
                i = i + 1
                novel_id = self.NovelList2api(title, novel['desc'], novel['author'], novel['category'], novel['links'],
                                novel['cover'], novel['cover_link'], novel['popularity'], novel['finished'],novel['cover'])
                pass
                if novel_id == False:
                    pass
                else:
                    self.Chapter2api(title, novel_id)
        return

    def CalcCNTChapterData(self, novel_title):
        chapter_cnt = self.mongodb.ChapterData.find({"title": novel_title}).count()
        print "《%s》小说章节数据为%s" % (novel_title,chapter_cnt)
        if (chapter_cnt > 0):
            self.mongodb.NovelData.update({"title": novel_title}, {'$set':{'got_cnt': chapter_cnt,'upfile':0}})
        else:
            self.mongodb.NovelData.update({"title": novel_title}, {
                                          '$set': {'got_cnt': 0, 'upfile': 0}})

    '''
    导入小说列表
    '''
    def NovelList2api(self, title, desc, author, category, links, cover, cover_link, popularity, finished,cover_file):
        res = self.http_post(self.api_base_url + 'add_novel?device_sn=import',{'title':title,'intro':desc,'author':author,'is_finish':1,'is_serial':1})
        if res['code'] == 0:
            # 更新封面图
            res2 = self.post(self.api_base_url + 'upload_pic2?device_sn=import',{'novel_id':res['novel_id']},{},cover_file)
            print res2
            return res['novel_id']
        else:
            return False

    '''
    导入小说的封面
    '''
    def NovelList2api(self, title, desc, author, category, links, cover, cover_link, popularity, finished,cover_file):
        res = self.http_post(self.api_base_url + 'add_novel?device_sn=import',{'title':title,'intro':desc,'author':author,'is_finish':1,'is_serial':1})
        if res['code'] == 0:
            # 更新封面图
            res2 = self.post(self.api_base_url + 'upload_pic2?device_sn=import',{'novel_id':res['novel_id']},{},cover_file)
            print res2
            return res['novel_id']
        else:
            return False

    '''
    导入小说章节
    '''
    def Chapter2api(self,title,novel_id):
        for chap in self.mongodb.ChapterData.find({'title': title}).sort('chapter_num'):
            print "《%s》 第%d章 %s"  % (title,chap['chapter_num'],chap['chapter'])
            res = self.http_post(self.api_base_url + 'add_chapter?device_sn=import', {
                                 'title': chap['chapter'], 'content': chap['chapter_text'], 'chap_num': chap['chapter_num'], 'novel_id': novel_id, 'words_number': len(chap['chapter_text'])})
            if res['code'] == 0:
                print "《%s》 第%d章 %s 上传成功" % (title, chap['chapter_num'], chap['chapter'])
            else:
                print "《%s》 第%d章 %s 上传失败" % (title, chap['chapter_num'], chap['chapter'])

        self.mongodb.NovelData.update({'title':title},{'$set':{'upfile':1}})
    def import2api(self,ifCalc=False):
        #更新小说的章节数
        self.CalcCNTNovelData(ifCalc)
        pass
 
    @staticmethod
    def http_get(url):
        req = urllib2.Request(url)

        res_data = urllib2.urlopen(req)
        res = res_data.read()
        print res

    @staticmethod
    def http_post(url,data):
        json = json_util.dumps(data)
        req = urllib2.Request(url = url)
        req.add_data(json)
        res_data = urllib2.urlopen(req)
        res = res_data.read()
        json2dict = json_util.loads(res)
        return json2dict

    def post(self,url,param_dict,param_header,file = '',param_type = 'x-www-form-urlencode'):
        '''
        @功能：封装post方式
        @paramType:指传入参数类型，可以是form-data、x-www-form-urlencode、json
        '''
        respone_code = None
        respone = None
        try:
            if param_type == 'x-www-form-urlencode':
                params = param_dict
            elif param_type == 'json':
                params = json.dumps(param_dict)

            if file == '' :
                ret = requests.post(url, data=params, headers=param_header)
            else:
                files = {'file':open(file,'rb')}
                ret = requests.post(url, data=params, headers=param_header,files = files)
            respone_code = ret.status_code
            respone = ret.text
        except requests.HTTPError, e:
            respone_code = e.getcode()
            respone = e.read().decode("utf-8")

        return respone_code,respone

    def __del__(self):
        self.mongo.close()
 
if __name__ == '__main__':
    multiprocessing.log_to_stderr()
    logger = multiprocessing.get_logger()
    logger.setLevel(logging.INFO)
 
    # args = sys.argv
    t1 = time.time()
    cls = Mongo2Api(logger)
    # 重新统计小说对应的章节数的时候需要开启,同时会重置小说上传标识
    # 所以建议有变更时再使用True参数
    # cls.import2api(True)
    cls.import2api()
